﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////

#pragma once

#include "IInteractionEventManager.h"
#include "ICommandInteractionEventArgs.h"
#include "UniIdentity.h"

namespace gcmp
{
    template<class Element, class Handler, class EventArgs>
    class InteractionEventHandlerRegisterHelper
    {
    public:
        InteractionEventHandlerRegisterHelper()
        {
            auto* pMgr = IInteractionEventManager::Get();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pMgr, L"interaction mgr is null", L"GMEP", L"2020-04-07");

            const std::wstring& eventTypeId = EventArgs::GetEventTypeId();
            const UniIdentity& implUid = Element::GetImplementationUid();
            bool added = pMgr->HasHandler(implUid, eventTypeId);
            if (!added)
            {
                OwnerPtr<Handler> opHandler = NEW_AS_OWNER_PTR(Handler);
                pMgr->AddHandler(implUid, eventTypeId, TransferOwnership(opHandler));
            }
        }

        ~InteractionEventHandlerRegisterHelper()
        {
        }
    };
}

#define REGISTER_INTERACTION_EVENT_HANDLER(ELEMENT_CLASS, HANDLER_CLASS, EVENT_ARGS_CLASS) \
namespace { \
    static gcmp::InteractionEventHandlerRegisterHelper<ELEMENT_CLASS, HANDLER_CLASS, EVENT_ARGS_CLASS> \
        s_##ELEMENT_CLASS##HANDLER_CLASS##InteractionEventHandlerRegisterHelper; \
}

